package com.jsy.user.controller;

import com.github.pagehelper.PageInfo;
import com.jsy.common.constant.StatusCode;
import com.jsy.common.util.TokenDecode;
import com.jsy.common.vo.Result;
import com.jsy.user.pojo.Address;
import com.jsy.user.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/****
 * @Author:www.jsy.com
 * @Description:
 * @Date www.jsy.com
 *****/

@RestController
@RequestMapping("/address")
@CrossOrigin
public class AddressController {

    @Autowired
    private AddressService addressService;

    /***
     * Address分页条件搜索实现
     * @param address: 查询条件
     * @param page:当前页
     * @param size:每页显示多少条
     * @return 查询结果
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo<Address>> findPage(@RequestBody(required = false) Address address, @PathVariable  int page, @PathVariable  int size){
        //调用AddressService实现分页条件查询Address
        PageInfo<Address> pageInfo = addressService.findPage(address, page, size);
        return new Result<PageInfo<Address>>("查询成功！", pageInfo);
    }

    /***
     * Address分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return 查询结果
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo<Address>> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用AddressService实现分页查询Address
        PageInfo<Address> pageInfo = addressService.findPage(page, size);
        return new Result<PageInfo<Address>>("查询成功！", pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param address 查询条件
     * @return 查询结果
     */
    @PostMapping(value = "/search" )
    public Result<List<Address>> findList(@RequestBody(required = false) Address address){
        //调用AddressService实现条件查询Address
        List<Address> list = addressService.findList(address);
        return new Result<List<Address>>("查询成功！", list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id 主键
     * @return 查询结果
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        //调用AddressService实现根据主键删除
        addressService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功！");
    }

    /***
     * 修改Address数据
     * @param address 更新对象
     * @return 操作结果
     */
    @PutMapping
    public Result update(@RequestBody Address address){
        //调用AddressService实现修改Address
        addressService.update(address);
        return new Result(true, StatusCode.OK, "修改成功！");
    }

    /***
     * 新增Address数据
     * @param address 新增对象
     * @return 操作结果
     */
    @PostMapping
    public Result add(@RequestBody Address address){
        //调用AddressService实现添加Address
        addressService.add(address);
        return new Result(true, StatusCode.OK, "添加成功！");
    }

    /***
     * 根据ID查询Address数据
     * @param id 主键
     * @return 查询结果
     */
    @GetMapping("/{id}")
    public Result<Address> findById(@PathVariable Integer id){
        //调用AddressService实现根据主键查询Address
        Address address = addressService.findById(id);
        return new Result<Address>("查询成功！", address);
    }

    /***
     * 查询Address全部数据
     * @return 查询结果
     */
    @GetMapping
    public Result<List<Address>> findAll(){
        //调用AddressService实现查询所有Address
        List<Address> list = addressService.findAll();
        return new Result<List<Address>>("查询成功！", list);
    }

    /***
     * 根据用户名查询Address
     * @return 查询结果
     */
    @GetMapping("/user/list")
    public Result<List<Address>> findAddressListByUsername(){
//        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        String username = TokenDecode.getUserInfo().get("username");
        Address address = new Address();
        address.setUsername(username);
        List<Address> list = addressService.findList(address);
        return new Result<List<Address>>("查询收件人地址列表成功！", list);
    }
}
